<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Adcategory extends Controller {

    public function action_list() {
        $pagination_arr = Paginationhelp::get_data('ads_category', 'id_category');
        $data = array(
            'result' => $pagination_arr['data'],
            'page_view' => $pagination_arr['pagination']
        );

        $view = Viewer::display('admin/modules/adcategory_list', $data);
        $this->response->body($view);
    }

    public function action_add() {
        if ($_POST) {
            $adcategory = Adcategory::instances()->set($_POST);
            if ($adcategory) {
                Message::set('广告添加成功！');
                Request::current()->redirect(URL::base() . 'admin/adcategory/add');
            }
        }
        $view = Viewer::display('admin/modules/adcategory_add');
        $this->response->body($view);
    }

    public function action_edit() {
       $id_category = $this->request->param('id');
        if ($_POST) {
            $adcategory = Adcategory::instances($id_category)->update($_POST);
            if ($adcategory) {
                Message::set('广告编辑成功！');
                Request::current()->redirect(URL::base() . 'admin/adcategory/edit/' . $id_category);
            }
        }
        $adcategory = Adcategory::instances($id_category)->get();
        $view = Viewer::display('admin/modules/adcategory_edit', array('data' => $adcategory));
        $this->response->body($view);
    }

    public function action_delete() {
        $id_category = $this->request->param('id');
        $flag = Newscategory::instances($id_category)->delete();
        if ($flag) {
            Message::set('分类删除成功');
            Request::current()->redirect(URL::base() . 'admin/newscategory/list');
        } else {
            Message::set('分类删除失败', 'error');
            Request::current()->redirect(URL::base() . 'admin/newscategory/list');
        }
    }

}